const mongoose = require("mongoose");
// js是弱类型语言，对数据的类型并不敏感
// 强类型语言 Java

// 等待值的接收
// 获取学生列表的当前页数据
const get = async ({ curPage, eachPage }) => {
    // 通过位运算符将类型转换为Number类型
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
    }
    // model传一个参数时表示获取这个model对象
    const shoppingModel = mongoose.model("shopping");
    // 获取当前model映射的那个集合里面存放的文档总数
    data.total = await shoppingModel.countDocuments();
    // Math.ceil 向上取整
    data.maxPage = Math.ceil(data.total / eachPage);
    // 计算跳过的条数
    const skipNum = (curPage - 1) * eachPage;
    // find是异步方法，populate(填充(找到相对应的完整数据返回))  skip(跳过多少条). limit(限制范围).exec=execute(执行)  sort({排序条件})
    data.rows = await shoppingModel.find().populate("shoppingid").populate("userid").skip(skipNum).limit(eachPage).sort({ userid: -1 }).exec();
    // 返回数据给路由
    return data;
}

// 新增学生
const addStudent = async (data ) => {
    // model传一个参数时表示获取这个model对象
    const shoppingModel = mongoose.model("shopping");
    const commsModel = mongoose.model("comms");
    const secondKillModel = mongoose.model("secondKill");
    const {num,userid,shoppingid }=data;
    const temp=await commsModel.findOne({_id:shoppingid})
    if(temp.isAttend){
        console.log("in");
        const {Sale}=await secondKillModel.findOne({CommId:shoppingid})
        console.log(Sale);
        return await shoppingModel.create({num,userid,shoppingid ,total:Number((Sale*num*temp.price).toFixed(2))});
    }
    // 添加学生数据
    return await shoppingModel.create({num,userid,shoppingid ,total:Number((num*temp.price).toFixed(2))});
}

const findOne = async _id => {
    // model传一个参数时表示获取这个model对象
    const shoppingModel = mongoose.model("shopping");
    // 添加学生数据
    return await shoppingModel.findOne({ _id });
}






// query查询条件   params需要修改的数据
const update = async (query, params) => {
    const shoppingModel = mongoose.model("shopping");
    console.log(query, params);
    // 修改数据
    return await shoppingModel.updateOne(query, params);
}


const delImgs = async ({ _id, url }) => {
    const shoppingModel = mongoose.model("shopping");
    // findOne找到匹配的数据立即返回，不会接着匹配数据
    const student = await shoppingModel.findOne({ _id });
    // 删除url对应的图片，并保存！！！！！sava
    student.imgs.pull(url);
    await student.save();
}




// 删除学生
const removeStudent = async ({ _id }) => {
    const shoppingModel = mongoose.model("shopping");
    // remove删除数据库_id匹配的数据
    await shoppingModel.remove({ _id });
}
// 修改学生
const changeStudent = async (newData) => {
    const shoppingModel = mongoose.model("shopping");
    await shoppingModel.update({ _id: newData._id }, newData);
    
}


// 通过用户id查找购物车
const findshop = async userid => {
    // model传一个参数时表示获取这个model对象
    console.log(userid);
    const shoppingModel = mongoose.model("shopping");
    // 添加学生数据
    return await shoppingModel.find({ userid }).populate("shoppingid").populate("userid").exec();
}






// 暴露接口
module.exports = {
    get,
    addStudent,
    removeStudent,
    changeStudent,
    findOne,
    update,
    delImgs,
    findshop
}
